// pages/fetchdata/index.js
import {
  getCurrenDay
} from '../../data/common'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fetchChooseDate: true,
    // 选择日期
    date: '',
    // 选择时间段
    index: '0',
    distance_time: '当日',
    tianshu: 1, //取当日
    picker: ['当日', '7天内', '15天内'],
    distances: [1, 7, 15],
    loadModal:false//显示加载框
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
  },
  init: function () {
    console.log(getCurrenDay())
    this.setData({
      date: getCurrenDay()
    })
  },
  switchChange: function (e) {
    this.setData({
      fetchChooseDate: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value,
      distance_time: this.data.picker[e.detail.value],
      tianshu: this.data.distances[e.detail.value]
    })
  },
  fetchData: function () {
    var that = this;
    this.showload(true)
    wx.cloud.callFunction({
      name: 'fetchdata',
      data: {
        riqi: this.data.fetchChooseDate ? this.data.date : '',
        tianshu: this.data.fetchChooseDate ? 0 : this.data.tianshu,
        websitecode: wx.getStorageSync('account')
      }
    }).then(res => {
      that.showload(false);
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: res.result.msg
      })

    }).catch(err => {
      console.log(err)
      that.showload(false);
      wx.showModal({
        title: '提示',
        showCancel: false,
        content:'获取数据失败'
      })
    })
  },
  showload:function(stats){
    this.setData({
      loadModal:stats
    })
  }
})